<?php
namespace Admin\Controller;

use Base\Controller\AdminBaseController;

class MenuController extends AdminBaseController {

    protected $menuModel;
	protected $userModel;
    protected $roleMenuModel;

    public function _initialize() {
        parent::_initialize();
        
        $this->menuModel = D("Admin/Menu");
		$this->userModel = D("Admin/User");
        $this->roleMenuModel = D("Admin/RoleMenu");
    }
	
	/******************************** JSON *********************************/
	/** 添加信息 */
	public function save() {
		$menuJson = file_get_contents('php://input');
		$menu = json_decode($menuJson, true);

		// 执行添加
		$result = $this->menuModel->add($menu);
		if (!$result) {
			$data['code'] = 499;
			$data['msg'] = '添加失败！';
			$this->ajaxReturn($data);
		}

		$newMenu = $this->menuModel->where(array("id" => $result))->find();
		
		$data['code'] = 0;
		$data['msg'] = '添加成功！';
		$data['obj'] = $newMenu;
		$this->ajaxReturn($data);
	}
	
	/** 查看所有  */
    public function getAll() {
		$result = $this->menuModel->order(array("sort_order" => "asc"))->select();

		$data['code'] = 0;
		$data['msg'] = '找到目标！';
		$data['obj'] = $result;
		$this->ajaxReturn($data);
	}
	 
	/** 查看某信息 */
	public function find() {
		$id = $_GET['id'];
		
		$menu = $this->menuModel->where(array("id" => $id))->find();
		if (!$menu) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
	    $data['code'] = 0;
	    $data['msg'] = 'OK';
	    $data['obj'] = $menu;
	    $this->ajaxReturn($data);
	}

	/** 修改某信息 */
	public function update() {
		$id = $_GET['id'];
		$menuJson = file_get_contents('php://input');
		$menu = json_decode($menuJson, true);
		
		// 执行更新
		$result = $this->menuModel->where(array("id" => $id))->save($menu);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '编辑失败！';
			$this->ajaxReturn($data);
		}
		
		$updatedMenu = $this->menuModel->where(array("id" => $id))->find();
		$data['code'] = 0;
		$data['msg'] = '编辑成功！';
		$data['obj'] = $updatedMenu;
		$this->ajaxReturn($data);
	}
	
	/** 排序 */
	public function sortOrder() {
		$result = parent::_sortOrder($this->menuModel);
		if (!$result) {
			$data['code'] = 499;
			$data['msg'] = '编辑排序失败！';
			$this->ajaxReturn($data);
		}

		$data['code'] = 0;
		$data['msg'] = '编辑排序成功！';
		$this->ajaxReturn($data);
	}
	
	/** 删除某信息 */
	public function delete() {
		$id = $_GET['id'];
		
		$user = $this->menuModel->where(array("id" => $id))->find();
		if (!$user) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		$count = $this->menuModel->where(array("pid" => $id))->count();
		if ($count > 0) {
			$data['code'] = 499;
			$data['msg'] = '该菜单下还有子类，无法删除！';
			$this->ajaxReturn($data);
		}
		
// 		$count = M('Content')->where(array("cid" => $id))->count();
// 		if ($count > 0) {
// 			$data['code'] = 499;
// 			$data['msg'] = '该分类下有内容，无法删除！';
// 			$this->ajaxReturn($data);
// 		}
		
		if (!$this->menuModel->delete($id)) {
			$data['code'] = 499;
			$data['msg'] = '删除失败！';
			$this->ajaxReturn($data);
		}

		$data['code'] = 0;
		$data['msg'] = '删除成功！';
		$this->ajaxReturn($data);
	}
	
	/** getMenuId4Role 分配menu选择用  */
    public function getMenuIds4Role() {
		$roleId = $_GET['roleId'];
		
		$menuIds = $this->roleMenuModel->where(array("role_id" => $roleId))->getField('menu_id', true);

		$data['code']  = 0;
    	$data['msg'] = 'ok';
    	$data['obj'] = $menuIds;
    	$this->ajaxReturn($data);
	}

	/******************************** HTML *********************************/
	// 分类列表
    public function index() {
		$this->display(':menu');
		return;
	}

}
